/*
 * Copyright 2013-2015 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xdtech.parking.protocol.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.common.utils.JsonUtil;
import com.xdtech.parking.model.ParkingDevice;
import com.xdtech.parking.protocol.vo.DeviceInfo;

/**
 * 
 * @author max.zheng
 * @create 2015-6-20下午2:51:54
 * @since 1.0
 * @see
 */
public class ParkRedis {
	private static RedisTemplate<String, String> rt = (RedisTemplate<String, String>) ApplicationContextUtil.getContext().getBean("redisTemplate");
	private static String AREA = "AREA_";
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-20下午8:42:12
	 * @modified by
	 * @param parkingDevice
	 */
	public static void setAreaDeviceInfo(ParkingDevice parkingDevice) {
		DeviceInfo deviceInfo = DeviceInfo.createItem(parkingDevice);
		rt.opsForHash().put(AREA+deviceInfo.getAid(), deviceInfo.getUkid(),JsonUtil.getJsonStr(deviceInfo));
	}
//	public static void setAreaDeviceInfo(String aid,String ukid, String value)
//	{
//		rt.opsForHash().put(AREA+aid, ukid,value);
//	}
	public static void setDeviceSetValue(String ukey,String setValue) {
		//设置set值
		rt.opsForList().rightPush(ukey+"_SET", setValue);
	}
	public static String getDeviceSetValue(String ukey) {
		//获取set值
		return rt.opsForList().rightPop(ukey+"_SET");
	}
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-20下午8:09:06
	 * @modified by
	 * @param aid
	 * @param ukid
	 * @return
	 */
	public static boolean checkDevIsExit(String aid, String ukid) {
		Object obj = rt.opsForHash().get(AREA+aid, ukid);
		return obj!=null;
	}
	
	public static DeviceInfo getDeviceInfo(String aid, String ukid) {
		String obj = (String) rt.opsForHash().get(AREA+aid, ukid);
		return (DeviceInfo) JsonUtil.getDtoFromJsonObjStr(obj, DeviceInfo.class);
	}
	public static void deleteDeviceInfo(String aid, String ukid) {
		rt.opsForHash().delete(AREA+aid, ukid);
	}
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-20下午9:31:28
	 * @modified by
	 * @param aid
	 * @return
	 */
	public static List<DeviceInfo> getDeviceInfosByArea(String aid) {
		Map<Object, Object> deviceMap = rt.opsForHash().entries(AREA+aid);
		List<DeviceInfo> deviceInfos = new ArrayList<DeviceInfo>();
		for (Object obj : deviceMap.values()) {
			deviceInfos.add((DeviceInfo) JsonUtil.getDtoFromJsonObjStr(obj.toString(), DeviceInfo.class));
		}
		return deviceInfos;
	}
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-20下午11:52:24
	 * @modified by
	 * @param areaCode
	 * @param deviceCode
	 * @param currentTimeMillis
	 */
	public static void carPark(String areaCode, String deviceCode,boolean isChangeCar) {
		long currentTimeMillis = System.currentTimeMillis();
		if (isChangeCar) {
			//有车了并且状态变更，开始停车时间
			rt.opsForHash().put(areaCode+"_"+deviceCode, "HAS_CAR_TIME", currentTimeMillis+"");
		}
		//心跳
		rt.opsForHash().put(areaCode+"_"+deviceCode, "HEART_TIME", currentTimeMillis+"");
	}
	/**
	 * 空闲车位
	 * @author max.zheng
	 * @create 2015-7-3下午10:11:31
	 * @modified by
	 * @param areaCode
	 * @param deviceCode
	 * @param hasCar
	 * @param isChangeCar
	 */
	public static void freePark(String areaCode, String deviceCode,boolean isChangeCar) {
		long currentTimeMillis = System.currentTimeMillis();
		if (isChangeCar) {
			//无车
			rt.opsForHash().put(areaCode+"_"+deviceCode, "HAS_CAR_TIME","-1");
		}
		//心跳
		rt.opsForHash().put(areaCode+"_"+deviceCode, "HEART_TIME", currentTimeMillis+"");
	}
	
	/**
	 * 获取开始停车时长，毫秒
	 * 
	 * @author max.zheng
	 * @create 2015-7-3下午10:22:00
	 * @modified by
	 * @param areaCode
	 * @param deviceCode
	 * @return
	 */
	public static long getParkTime(String areaCode, String deviceCode) {
		String time = (String) rt.opsForHash().get(areaCode+"_"+deviceCode, "HAS_CAR_TIME");
		if (StringUtils.isNotBlank(time)&&!"-1".equals(time)) {
			//计算停车时长，是毫秒
			return Long.valueOf(time);
		}else {
			return 0L;
		}
	}
	

}
